import { asyncRouterMap, constantRouterMap } from '@/router'
import Layout from '@/views/layout/Layout'

/**
 * 通过meta.role判断是否与当前用户权限匹配
 * @param roles
 * @param route
 */
// function hasPermission(roles, route) {
//   if (route.meta && route.meta.roles) {
//     return roles.some(role => route.meta.roles.includes(role))
//   } else {
//     return true
//   }
// }

/**
 * 递归过滤异步路由表，返回符合用户角色权限的路由表
 * @param routes asyncRouterMap
 * @param roles
 */
// function filterAsyncRouter(routes, roles) {
//   const res = []

//   routes.forEach(route => {
//     const tmp = { ...route }
//     if (hasPermission(roles, tmp)) {
//       if (tmp.children) {
//         tmp.children = filterAsyncRouter(tmp.children, roles)
//       }
//       res.push(tmp)
//     }
//   })

//   return res
// }

const permission = {
  state: {
    routers: constantRouterMap,
    addRouters: []
  },
  mutations: {
    SET_ROUTERS: (state, routers) => {
      state.addRouters = routers
      state.routers = constantRouterMap.concat(routers)
    }
  },
  actions: {
    GenerateRoutes({ commit }, data) {
      return new Promise(resolve => {
        const { roles } = data
        // 对数据做处理,模拟路由表展示动态加载数据
        for (var i = 0; i < roles.length; i++) {
          const file = roles[i].url // 没有子路由的文件路径
          if (roles[i].children.length > 1) { // 判断是否包含子路由,如果有展示子路由,否则按正常展示
            var childrenlist = [] // 定义一个数组接收子元素路由
            for (var j = 0; j < roles[i].children.length; j++) { // 循环有子路由的数据进行push
              const childfile = roles[i].children[j].url // 有子路由的情况下的文件的地址
              childrenlist.push({ // 把子路由数据push到数组,然后给到最外层的路由
                path: roles[i].children[j].url,
                component: () => import(`@/views/${childfile}/index`),
                name: roles[i].children[j].name,
                meta: { title: roles[i].children[j].name, icon: 'dashboard1', noCache: true }
              })
            }
            asyncRouterMap.push({
              path: '',
              component: Layout,
              redirect: roles[i].url,
              name: roles[i].name,
              meta: {
                title: roles[i].name,
                icon: 'example'
              },
              children: childrenlist
            })
          } else {
            asyncRouterMap.push({
              path: '',
              component: Layout,
              redirect: roles[i].url,
              children: [
                {
                  path: roles[i].url,
                  component: () => import(`@/views/${file}/index`),
                  name: roles[i].name,
                  meta: { title: roles[i].name, icon: 'dashboard', noCache: true }
                }
              ]
            })
          }
        }
        // if (roles.includes('admin')) {
        //   accessedRouters = asyncRouterMap
        // } else {
        //   accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
        // }
        commit('SET_ROUTERS', asyncRouterMap)
        resolve()
      })
    }
  }
}

export default permission

